package org.example.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadDemo implements Runnable {
    private int tips;

    public ThreadDemo(int tips) {
        this.tips = tips;
    }

    @Override
    public void run() {
        Lock lock = new ReentrantLock();
        while (true) {
            try {
            lock.lock();
                if (tips == 0) {
                    System.out.println(Thread.currentThread().getName()+"：票卖完了");
                    break;
                }
                tips--;
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName() + "卖出了一张票，还剩" + tips + "张票");
//            lock.unlock();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }finally {
                lock.unlock();
            }
        }
    }
}
